﻿var positionContentTypeID = "0x0100778DF92A0172F041944DD13435237073";


// This function enables/disables "Close Position" ribbon button
function CanClosePosition()
{
	var selectedItems = SP.ListOperation.Selection.getSelectedItems();

	//check if exactly one item is selected
	if (CountDictionary(selectedItems) == 1)
	{
		if (this.itemId != selectedItems[0]['id'])
		{
			this.itemId = selectedItems[0]['id'];
			GetPositionClosedOnDate(this.itemId);
		}
		else if (this.ContentTypeIDCurrent == null)
		{
			return false;
		}
		// proceed only if content type id match
		else if (this.ContentTypeIDCurrent.indexOf(positionContentTypeID) == 0)
		{
			if (this.ClosedOn == null)
			{
				return true;
			}
			else if (this.ClosedOn != null)
			{
				return false;
			}
		}

	}
	return false;
}

// This function enables/disables "Open Position" ribbon button
function CanOpenPosition()
{
	var selectedItems = SP.ListOperation.Selection.getSelectedItems();

	//check if exactly one item is selected
	if (CountDictionary(selectedItems) == 1)
	{
		if (this.itemId != selectedItems[0]['id'])
		{
			this.itemId = selectedItems[0]['id'];
			GetPositionClosedOnDate(this.itemId);
		}
		else if (this.ContentTypeIDCurrent == null)
		{
			return false;
		}
		// proceed only if content type id match
		else if (this.ContentTypeIDCurrent.indexOf(positionContentTypeID) == 0)
		{
			if (this.ClosedOn == null)
			{
				return false;
			}
			else if (this.ClosedOn != null)
			{
				return true;
			}
		}

	}
	return false;
}

// This function enables/disables "Copy Position" ribbon button
function CanCopyPosition()
{
	var selectedItems = SP.ListOperation.Selection.getSelectedItems();

	//check if exactly one item is selected
	if (CountDictionary(selectedItems) == 1)
	{
		if (this.itemId != selectedItems[0]['id'])
		{
			this.itemId = selectedItems[0]['id'];
			GetPositionClosedOnDate(this.itemId);
		}
		else if (this.ContentTypeIDCurrent == null)
		{
			return false;
		}
		// proceed only if content type id match
		else if (this.ContentTypeIDCurrent.indexOf(positionContentTypeID) == 0)
		{
			return true;
		}
	}

	return false;
}

// This function gets "Closed On" field value of position item of given item ID
function GetPositionClosedOnDate(itemId)
{
	var clientContext = SP.ClientContext.get_current();
	var currentList = clientContext.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
	this.singleItem = currentList.getItemById(itemId);
	
	clientContext.load(this.singleItem, 'ClosedOn');
	clientContext.load(this.singleItem, 'ContentType');
	clientContext.executeQueryAsync(Function.createDelegate(this, this.OnSucceeded_GetPositionClosedOnDate), Function.createDelegate(this, this.OnFailed_GetPositionClosedOnDate));
}

// Handler for success calling of GetPositionClosedOnDate function
function OnSucceeded_GetPositionClosedOnDate()
{
	// get content type ID
	this.ContentTypeIDCurrent = this.singleItem.get_contentType().get_id().$V_1;

	// get Close On
	this.ClosedOn = this.singleItem.get_item('ClosedOn');

	RefreshCommandUI();
}

// Handler for failed calling of GetPositionClosedOnDate function
function OnFailed_GetPositionClosedOnDate(sender, args)
{
	alert('Error occurred GetPositionClosedOnDate: ' + args.get_message());
}

